package com.niit.agricstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.niit.agricstore.entity.Product;
import com.niit.agricstore.entity.User;
import com.niit.agricstore.mapper.UserMapper;
import com.niit.agricstore.service.UserService;
import com.niit.agricstore.util.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl  extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> findAllUsers() {
        return userMapper.selectAll();
    }
    public PageInfo<User> findUserAllByPage(Integer pageNum, Integer pageSize) {
        Page<User> page = new Page<User>(pageNum,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_is_admin",0);
        queryWrapper.orderByDesc("user_create_time");
        page = userMapper.selectPage(page,queryWrapper);
        List<User> userlist = page.getRecords();
        return PageInfo.of(userlist);
    }

    @Override
    public PageInfo<User> findUserNameByPage(int pageNum, int pageSize, String name) {
        Page<User> page = new Page<User>(pageNum,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("user_name",name);
        page = userMapper.selectPage(page,queryWrapper);
        List<User> userlist = page.getRecords();
        return PageInfo.of(userlist);
    }

    @Override
    public PageInfo<User> findUserIdByPage(int pageNum, int pageSize, Long id) {
        Page<User> page = new Page<User>(pageNum,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("user_id",id);
        page = userMapper.selectPage(page,queryWrapper);
        List<User> userlist = page.getRecords();
        return PageInfo.of(userlist);
    }

    @Override
    public int deleteUser(Long id) {
        User user = new User();
        user.setUserId(id);
        user.setUserIsDelete(1);
        return userMapper.updateById(user);
    }

    @Override
    public int updateLoginTime(Long id, String phone) {
        return 0;
    }

    @Override
    public int updatePhone(Long id, String phone) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        User user = userMapper.selectOne(queryWrapper);
        user.setUserPhone(phone);
        return userMapper.updateById(user);
    }

    @Override
    public PageInfo<User> findUserPhoneByPage(int pageNum, int pageSize, String phone) {
        Page<User> page = new Page<User>(pageNum,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.likeRight("phone",phone);
        page = userMapper.selectPage(page,queryWrapper);
        List<User> userlist = page.getRecords();
        return PageInfo.of(userlist);
    }

    @Override
    public User getCurrentUser(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User loginCheck(String username ,String password) {
        QueryWrapper<User> nameQueryWrapper = Wrappers.query();
        nameQueryWrapper.eq("user_name", username);
        if (userMapper.selectCount(nameQueryWrapper)==1){
            User user = userMapper.loginCheck(username, password);
            // 获取当前时间
            Date now = new Date();
            // 设置用户最后更新时间为当前时间
            user.setUserLoginTime(now);
            // 更新用户信息（这里假设有相应的方法可以更新用户信息）
            userMapper.updateById(user);
            return user;
        }
            return userMapper.loginCheck(username,password);
        }
    @Override
    public String userRegister(User user){
//        // 检查用户名长度
//        if (user.getUserName().length() < 3 || user.getUserName().length() > 20) {
//            return "用户名长度应在3~20之内";
//        }
//
//        // 检查密码长度和格式
//        String password = user.getUserPassword();
//        if (password.length() < 8 || password.length() > 16 || !PasswordUtils.hasDigitAndLetter(password)) {
//            return "密码长度应在8~16个字符之间，且应同时有数字与英文字母";
//        }
        // 检查用户名是否已存在
        QueryWrapper<User> nameQueryWrapper = Wrappers.query();
        nameQueryWrapper.eq("user_name", user.getUserName());
        if (userMapper.selectCount(nameQueryWrapper) > 0) {
            return "用户名已存在！";
        }
        QueryWrapper<User> phoneQueryWrapper = Wrappers.query();
        phoneQueryWrapper.eq("user_phone", user.getUserPhone());
        if (userMapper.selectCount(phoneQueryWrapper) > 0) {
            return "手机号已存在！";
        }
        // 保存用户信息
        userMapper.insert(user);
        return "注册成功";
    }

    @Override

    public int updatePassword(Long id, String newpass) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        User user = userMapper.selectOne(queryWrapper);
        System.out.println("------------------------"+user);
        user.setUserPassword(newpass);
        return userMapper.updateById(user);
    }
}
